<template>
  <!-- 公告组件 -->
  <div class="image-content">
    <p class="desc">添加公告 (最多{{ len }}条，可拖动排序）</p>
    <vuedraggable v-model="list.data" tag="ul" draggable="li" v-if="list.data && list.data.length > 0"
      class="image-list" :class="{ disable: data.tabType == 2 }">
      <li v-for="(item, index) in list.data" :key="index">
        <div class="l-info">
          <p>
            <span class="sort">排序{{ index + 1 }}</span>
          </p>
          <p>
            <span>公告抬头：</span>
            <el-input v-model="list.data[index].title"></el-input>
          </p>
          <p>
            <span>发布人：</span>
            <el-input v-model="list.data[index].publisher"></el-input>
          </p>
          <p>
            <span>内容：</span>
            <el-input type="textarea" v-model="list.data[index].content"></el-input>
          </p>
          <p>
            <span>时间：</span>
            <el-input v-model="list.data[index].time" disabled></el-input>
          </p>
        </div>
        <div class="r-image">
          <span @click="removeImage(index)" class="el-icon-close"></span>
        </div>
      </li>
    </vuedraggable>
    <template v-if="list.data && list.data.length < len">
      <el-button type="primary" icon="el-icon-plus" @click="addImage(null)" class="add-image">
        添加
      </el-button>
    </template>
  </div>
</template>

<script>
import vuedraggable from "vuedraggable";
export default {
  name: "notice",
  components: {
    vuedraggable,
  },
  props: {
    data: {
      type: Object,
      default: () => { },
    },
  },
  data() {
    return {
      list: {},
      url: "",
      index: null,
      show: false,
      imageIndex: null,
      link: "",
      time: '',
    };
  },
  computed: {
    size() {
      return this.list.type == "images" ? "750*750" : "750*400";
    },
    len() {
      return this.list.type == "images" ? 8 : 10;
    },
  },
  mounted() {
    this.list = this.data;
    this.time = this.formatDate(new Date())
    this.list.data.forEach((item, index) => {
      if (!this.list.data[index].time) {
        this.$set(this.list.data[index], 'time', this.time)
      }
    })
  },
  methods: {

    formatDate(date) {
      var date = new Date(date)
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hour = date.getHours();
      var minute = date.getMinutes();
      var second = date.getSeconds();
      return year + "-" + this.formatTen(month) + "-" + this.formatTen(day) + " " + this.formatTen(hour) + ":" + this.formatTen(minute) + ":" + this.formatTen(second);
    },
    formatTen(num) {
      return num > 9 ? (num + "") : ("0" + num);
    },

    removeImage(index) {
      this.list.data.splice(index, 1);
    },
    addImage(index) {
      const data = {
        title: "",
        publisher: "",
        content: "",
        time: this.formatDate(new Date())
      };
      this.list.data.push(data);
      this.$set(this.list.data, index, data);
    },

  },
};
</script>

<style lang="scss" scoped>
.image-content {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  height: 100%;
  overflow: hidden;

  .desc {
    text-align: center;
    font-size: 12px;
    color: #666;
    margin: 18px 0;
    padding-bottom: 10px;
    border-bottom: 1px dashed #ddd;
  }

  .size {
    text-align: center;
    font-size: 12px;
    color: #999;
    margin-bottom: 0;
  }

  .add-image {
    width: calc(100% - 20px);
    height: 34px;
    line-height: 34px;
    padding: 0;
    font-size: 12px;
    margin-left: 10px;
    margin-top: 10px;
  }

  .image-list {
    margin: 0;
    padding: 0 10px;
    overflow: auto;

    &::-webkit-scrollbar-thumb {
      background: #dbdbdb;
      border-radius: 10px;
    }

    &::-webkit-scrollbar-track {
      background: #f6f6f6;
      border-radius: 10px;
    }

    &::-webkit-scrollbar {
      width: 6px;
    }

    li {
      list-style: none;
      background: #f7f8f9;
      border-radius: 4px;
      padding: 6px 14px 10px;
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;

      &.disable div {
        pointer-events: none;
        user-select: none;
      }

      .l-info {
        font-size: 12px;
        padding-top: 8px;
        width: calc(100% - 70px);

        p {
          margin: 12px 0 0;
          white-space: nowrap;
          overflow: hidden;
          display: flex;

          .link {
            color: #1b8bff;
            cursor: pointer;
          }

          .text {
            white-space: nowrap;
            text-align: -webkit-auto;
            text-overflow: ellipsis;
            overflow: hidden;
          }
        }
      }

      .r-image {
        text-align: right;

        .el-icon-close {
          color: #999;
          font-size: 12px;
          font-weight: 600;
          margin-bottom: 6px;
          cursor: pointer;

          &:hover {
            color: red;
          }
        }

        .image-box {
          width: 70px;
          height: 70px;
          border-radius: 5px;
          overflow: hidden;
          position: relative;
          background: #fff;
          display: flex;
          justify-content: center;
          align-items: center;

          &::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            opacity: 0;
            transition: all 0.3s;
          }

          span {
            position: absolute;
            top: 50%;
            left: 50%;
            color: #fff;
            transform: translate(-50%, -50%);
            font-size: 20px;
            cursor: pointer;
            opacity: 0;
            transition: all 0.3s;
          }

          img {
            max-width: 100%;
          }

          &:hover {

            &::before,
            span {
              opacity: 1;
            }
          }
        }
      }
    }
  }
}
</style>
